<?php

defined('SYSPATH') or die('Access error!');

class Controller_Shop_User_Goods extends Controller_Shop_User_Main
{

    protected $goods;
    protected $where = array();

    public function before()
    {
        parent::before();
        $this->goods = Model::factory('shop_goods');
        Assets::css('base', $this->template->media_path . 'css/base.css');
        I18n::set_current_file('shop');
        $this->where['user_id'] = $this->user_detail['id'];
        $this->list_operate = $this->request->action;

        $this->template->assign('operate_type', $this->request->action);
        $this->template->assign('user_shop_nav', $this->user_shop_seller_nav());
    }

    public function goods_list()
    {
        //当前位置
        $this->set_define_position(__('seller manager'));
        Breadcrumbs::add(__($this->list_operate . ' goods'));
        return $this->goods->get_base_list($this->where);
    }

    public function display_goods_list()
    {
        $goods_list = $this->goods_list();
        $this->template->assign('goods_list', $goods_list['list']);
        $this->template->assign('page', $goods_list['page']);
        $this->template->view('shop_goods_list');
    }

    public function check_user_goods($id)
    {
        if (empty($id) || !$this->goods->is_exist(array('id' => $id, 'user_id' => $this->user_detail['id'])))
        {
            Message::error(__('no exist'));
            $this->request->redirect(URL::site('shop/seller/goods/onsale'));
        }
    }

    public function action_comment()
    {
        $id = $this->request->param('id');
        if (empty($id))
        {
            Message::error(__('goods id is empty'));
            $this->request->redirect('shop/buyer/order/list');
        }
        Assets::js('jrating', $this->template->media_path . 'js/rate/jquery.raty.min.js');
        if ($_POST)
        {
            $data = array();
            $data['user_id'] = $this->user_detail['id'];
            $data['email'] = $this->user_detail['email'];
            $data['username'] = $this->user_detail['username'];
            $data['add_time'] = Date::get_time();
            $data['credit'] = Arr::get($_POST, 'credit', '');
            $data['content'] = Html::get_safe_content(Arr::get($_POST, 'content', ''));
            $data['goods_id'] = $id;
            if ($data['credit'] == '' || $data['content'] == '')
            {
                Message::error(__('comments contents is empty'));
                $this->request->redirect('shop/user/goods/comment');
            }

            if (Model::factory('shop_goods_comments')->insert($data))
            {
                Message::success(__('comments is success'));
                $this->request->redirect('shop/buyer/order/list');
            }
        }
        $this->template->assign('id', $id);
        $this->template->view('shop_goods_comment');
    }

    public function action_reply()
    {
        $id = $this->request->param('id');
        if ($id == '')
        {
            Message::error(__('error'));
            $this->request->redirect('shop/seller/order/list');
        }
        $comments_detail = Model::factory('shop_goods_comments')->get_row(array('id' => $id), 'id,content,email,username,add_time');
        if ($_POST)
        {
            $data = array();
            $data['reply_time'] = Date::get_time();
            $data['reply_content'] = Html::get_safe_content(Arr::get($_POST, 'content', ''));
            if (Model::factory('shop_goods_comments')->update($data, array('id' => $id)))
            {
                Message::success(__('reply is success'));
                $this->request->redirect('shop/seller/order/list');
            }
        }
        $this->template->assign('data', $comments_detail);
        $this->template->view('shop_goods_reply');
    }

}